package com.hzy.ums.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hzy.wechat.feign.WxAppletFeign;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.hzy.ums.domain.bo.MemberWechatBo;
import com.hzy.ums.domain.vo.MemberWechatVo;
import com.hzy.ums.domain.MemberWechat;
import com.hzy.ums.mapper.MemberWechatMapper;
import com.hzy.ums.service.IMemberWechatService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户微信信息Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-19
 */
@Slf4j
@RequiredArgsConstructor
public class MemberWechatServiceImpl implements IMemberWechatService {

    private final MemberWechatMapper baseMapper;

    /**
     * 查询用户微信信息
     */
    @Override
    public MemberWechatVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户微信信息列表
     */
    @Override
    public TableDataInfo<MemberWechatVo> queryPageList(MemberWechatBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MemberWechat> lqw = buildQueryWrapper(bo);
        Page<MemberWechatVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户微信信息列表
     */
    @Override
    public List<MemberWechatVo> queryList(MemberWechatBo bo) {
        LambdaQueryWrapper<MemberWechat> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MemberWechat> buildQueryWrapper(MemberWechatBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MemberWechat> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMemberId() != null, MemberWechat::getMemberId, bo.getMemberId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionid()), MemberWechat::getUnionid, bo.getUnionid());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), MemberWechat::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getRoutineOpenid()), MemberWechat::getRoutineOpenid, bo.getRoutineOpenid());
        lqw.eq(bo.getGroupid() != null, MemberWechat::getGroupid, bo.getGroupid());
        lqw.eq(StringUtils.isNotBlank(bo.getTagidList()), MemberWechat::getTagidList, bo.getTagidList());
        lqw.eq(bo.getSubscribe() != null, MemberWechat::getSubscribe, bo.getSubscribe());
        lqw.eq(bo.getSubscribeTime() != null, MemberWechat::getSubscribeTime, bo.getSubscribeTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSessionKey()), MemberWechat::getSessionKey, bo.getSessionKey());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessToken()), MemberWechat::getAccessToken, bo.getAccessToken());
        lqw.eq(bo.getExpiresIn() != null, MemberWechat::getExpiresIn, bo.getExpiresIn());
        lqw.eq(StringUtils.isNotBlank(bo.getRefreshToken()), MemberWechat::getRefreshToken, bo.getRefreshToken());
        lqw.eq(bo.getExpireTime() != null, MemberWechat::getExpireTime, bo.getExpireTime());
        return lqw;
    }

    /**
     * 新增用户微信信息
     */
    @Override
    public Boolean insertByBo(MemberWechatBo bo) {
        MemberWechat add = BeanUtil.toBean(bo, MemberWechat.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户微信信息
     */
    @Override
    public Boolean updateByBo(MemberWechatBo bo) {
        MemberWechat update = BeanUtil.toBean(bo, MemberWechat.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MemberWechat entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户微信信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
